﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Visitor;

namespace ASTClass
{
    class WriteAST : OperatorAST
    {
        public WriteAST() : base() { name = "Write"; }
        public WriteAST(AST parent) : base(parent) { name = "Write"; }

        public override AST Clone()
        {
            WriteAST ast = new WriteAST();
            ast.operators = operators;
            foreach (AST c in children)
                ast.children.Add(c.Clone());

            return ast;
        }

        #region Visitor pattern

        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }

        #endregion Visitor pattern

        public override void CodeGen(Taste.SymbolTable symTab, Taste.CodeFileGen cfg)
        {
            cfg.AddComment("---- Write Statement ----");
            foreach (AST c in children)
            {
                c.CodeGen(symTab, cfg);

                if (c.Name != "String")
                {
                    cfg.AddCommand("lw", "$v0, 0($sp)");
                    cfg.AddCommand("move", "$a0, $v0");
                    cfg.AddCommand("li", "$v0, 1");
                    cfg.AddCommand("syscall", string.Empty);
                }
                cfg.AddCommand("addiu", "$sp, $sp, 4");
            }
        }
    }
}
